package com.beyondsoft.sabg.edu.service.dataanalysis.impl;

import com.aliyun.dataanalysisgbi20240823.models.*;
import com.aliyun.teautil.models.RuntimeOptions;
import com.beyondsoft.sabg.edu.config.AnalysisConfig;
import com.beyondsoft.sabg.edu.model.dto.BusinessLogicDto;
import com.beyondsoft.sabg.edu.service.dataanalysis.BusinessLogicService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 业务逻辑服务实现类
 */
@Service
@Slf4j
public class BusinessLogicServiceImpl implements BusinessLogicService {

    @Autowired
    private com.aliyun.dataanalysisgbi20240823.Client analysisClient;

    @Autowired
    private AnalysisConfig analysisConfig;

    @Override
    public String createBusinessLogic(BusinessLogicDto businessLogicDto) throws Exception {
        log.info("创建业务逻辑，描述：{}", businessLogicDto.getDescription());

        try {
            CreateBusinessLogicRequest request = new CreateBusinessLogicRequest()
                    .setWorkspaceId(analysisConfig.getWorkspaceId())
                    .setType(businessLogicDto.getType())
                    .setDescription(businessLogicDto.getDescription());

            RuntimeOptions runtime = new RuntimeOptions();
            Map<String, String> headers = new HashMap<>();

            CreateBusinessLogicResponse response = analysisClient
                    .createBusinessLogicWithOptions(request, headers, runtime);

            log.info("业务逻辑创建完成，请求ID：{}", response.getBody().getRequestId());

            if (response.getStatusCode() == 200) {
                return "业务逻辑创建成功，请求ID：" + response.getBody().getRequestId();
            } else {
                throw new RuntimeException("创建业务逻辑失败：" + response.getBody().getErrorMsg());
            }

        } catch (Exception e) {
            log.error("创建业务逻辑失败", e);
            throw new RuntimeException("创建业务逻辑失败：" + e.getMessage(), e);
        }
    }

    @Override
    public Object listBusinessLogic(String nextToken, Integer maxResults) throws Exception {
        log.info("获取业务逻辑列表");

        try {
            ListBusinessLogicRequest request = new ListBusinessLogicRequest()
                    .setWorkspaceId(analysisConfig.getWorkspaceId())
                    .setNextToken(nextToken)
                    .setMaxResults(maxResults);

            RuntimeOptions runtime = new RuntimeOptions();
            Map<String, String> headers = new HashMap<>();

            ListBusinessLogicResponse response = analysisClient
                    .listBusinessLogicWithOptions(request, headers, runtime);

            log.info("业务逻辑列表获取完成，请求ID：{}", response.getBody().getRequestId());

            if (response.getStatusCode() == 200) {
                // 这里可以根据需要格式化返回数据
                return response.getBody().getData();
            } else {
                throw new RuntimeException("获取业务逻辑列表失败：" + response.getBody().getErrorMsg());
            }

        } catch (Exception e) {
            log.error("获取业务逻辑列表失败", e);
            throw new RuntimeException("获取业务逻辑列表失败：" + e.getMessage(), e);
        }
    }

    @Override
    public String updateBusinessLogic(BusinessLogicDto businessLogicDto) throws Exception {
        log.info("修改业务逻辑，ID：{}", businessLogicDto.getBusinessLogicIdKey());

        try {
            UpdateBusinessLogicRequest request = new UpdateBusinessLogicRequest()
                    .setWorkspaceId(analysisConfig.getWorkspaceId())
                    .setBusinessLogicIdKey(businessLogicDto.getBusinessLogicIdKey())
                    .setDescription(businessLogicDto.getDescription())
                    .setType(businessLogicDto.getType().longValue());

            RuntimeOptions runtime = new RuntimeOptions();
            Map<String, String> headers = new HashMap<>();

            UpdateBusinessLogicResponse response = analysisClient
                    .updateBusinessLogicWithOptions(request, headers, runtime);

            log.info("业务逻辑修改完成，请求ID：{}", response.getBody().getRequestId());

            if (response.getStatusCode() == 200) {
                return "业务逻辑修改成功，请求ID：" + response.getBody().getRequestId();
            } else {
                throw new RuntimeException("修改业务逻辑失败：" + response.getBody().getErrorMsg());
            }

        } catch (Exception e) {
            log.error("修改业务逻辑失败", e);
            throw new RuntimeException("修改业务逻辑失败：" + e.getMessage(), e);
        }
    }

    @Override
    public String batchDeleteBusinessLogic(List<String> BusinessLogicIdKeys) throws Exception {
        log.info("批量删除业务逻辑解释，数量：{}", BusinessLogicIdKeys.size());

        try {
            DeleteBusinessLogicRequest request = new DeleteBusinessLogicRequest()
                    .setBusinessLogicIdKeys(BusinessLogicIdKeys)
                    .setWorkspaceId(analysisConfig.getWorkspaceId());

            RuntimeOptions runtime = new RuntimeOptions();
            Map<String, String> headers = new HashMap<>();
            DeleteBusinessLogicResponse response = analysisClient.deleteBusinessLogicWithOptions(request, headers, runtime);


            log.info("业务逻辑解释批量删除完成，请求ID：{}", response.getBody().getRequestId());

            if (response.getStatusCode() == 200) {
                return "业务逻辑解释批量删除成功，请求ID：" + response.getBody().getRequestId();
            } else {
                throw new RuntimeException("批量删除业务逻辑解释失败：" + response.getBody().getErrorMsg());
            }

        } catch (Exception e) {
            log.error("批量删除业务逻辑解释失败", e);
            throw new RuntimeException("批量删除业务逻辑解释失败：" + e.getMessage(), e);
        }
    }
} 